package com.css.dzj.zfjg.modules.adminspection.service.Impl;

import com.css.dzj.zfjg.common.util.*;
import com.css.dzj.zfjg.modules.adminspection.entity.JgJgxwCheckActionEntity;
import com.css.dzj.zfjg.modules.adminspection.entity.QJgJgxwCheckActionEntity;
import com.css.dzj.zfjg.modules.adminspection.mapper.checkMapper;
import com.css.dzj.zfjg.modules.adminspection.service.JgJgxwCheckActionService;
import com.css.dzj.zfjg.modules.jcdx.entity.JgLhjgJcdxEntity;
import com.css.dzj.zfjg.modules.jcdx.entity.QJgLhjgJcdxEntity;
import com.css.dzj.zfjg.modules.taskinfo.vo.rwjgLh;
import com.css.dzj.zfjg.sys.user.entity.LoginUser;
import com.querydsl.core.QueryResults;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 行政检查行为信息实现层
 * @author wt
 * @date 2020\2\19 0019
 */
@Service
public class JgJgxwCheckActionServiceImpl implements JgJgxwCheckActionService {

    @Autowired
    private checkMapper kMapper;

    @Autowired
    private JPAQueryFactory jpaQueryFactory;

    @Autowired
    private ServiceUtil serviceUtil;

    @Autowired
    HttpServletRequest request;

    /**
     * 添加行政检查行为信息
     */
    @Override
    @Transactional
    public void add(JgJgxwCheckActionEntity checkActionEntity) {

        try {
            //创建时间
            checkActionEntity.setCreateTime(new Date());
            //插入时间
            checkActionEntity.setCdTime(new Date());
            checkActionEntity.setId(SnowflakeId.getId());
            //cd_id
            checkActionEntity.setCdId(SnowflakeId.getId());
            //创建人
            R r = serviceUtil.getUserInfoByRequest(request);
            if(r.getCode().equals("200")){
                LoginUser userInfo =  (LoginUser)r.get("data");
                checkActionEntity.setCreateUser(userInfo.getLoginName());
            }
            //记录唯一标识
            checkActionEntity.setRecordUniqueIdentity(SnowflakeId.getId());
            //新增数据类型i-insert,u-update,d-delete
            checkActionEntity.setCdOperation("i");
            //检查行为编码
            checkActionEntity.setCheckActionCode(SnowflakeId.getId());
            //状态0是未删除，1是已删除
            checkActionEntity.setFlag(Constant.NO_DELETE);
            //来源
            checkActionEntity.setCdSource("07");
            kMapper.save(checkActionEntity);
        } catch(Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 修改行政检查行为信息
     */
    @Override
    @Transactional
    public void update(JgJgxwCheckActionEntity checkActionEntity) {

    }

    /**
     * 通过id查询行政检查行为信息
     */
    @Override
    public JgJgxwCheckActionEntity findById(String id) {
        if(StringUtils.isEmpty(id)){

            throw new RuntimeException("参数为空");
        }
        return kMapper.findById(id).get();
    }

    /**
     * 分页查询行政检查行为信息
     */
    @Override
    public QueryResults<JgJgxwCheckActionEntity> queryPage(Pageable pageable) {
        QJgJgxwCheckActionEntity qCheck = QJgJgxwCheckActionEntity.jgJgxwCheckActionEntity;
        QueryResults<JgJgxwCheckActionEntity> checkResults = jpaQueryFactory.selectFrom(qCheck)
                .where(qCheck.flag.eq(Constant.NO_DELETE))
                .orderBy(qCheck.createTime.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
        return checkResults;
    }

    /**
     * 删除行政检查行为信息
     */
    @Override
    @Transactional
    public Boolean delete(String id) {
        QJgJgxwCheckActionEntity qCheck = QJgJgxwCheckActionEntity.jgJgxwCheckActionEntity;
        if(StringUtils.isEmpty(id)){
            return  false;
        }
        try{
            jpaQueryFactory
                    .update(qCheck)
                    .set(qCheck.flag,Constant.DELETE)
                    .where(
                            qCheck.flag.eq(Constant.NO_DELETE),
                            qCheck.id.eq(id)
                    )
                    .execute();
            return true;
        }catch (Exception e){
            return false;
        }
    }
    /**
     *根据编码查询行为信息
     */
    @Override
    public rwjgLh findOneById(String actionId) {
        if(StringUtils.isEmpty(actionId)) {
            return null;
        }
        QJgJgxwCheckActionEntity q = QJgJgxwCheckActionEntity.jgJgxwCheckActionEntity;
        JgJgxwCheckActionEntity fetchOne = jpaQueryFactory
                .selectFrom(q)
                .where(q.id.eq(actionId),
                        q.cdOperation.eq(CvaConstants.UN_DELETED_FLAG)
                )
                .fetchOne();
        rwjgLh vo = new rwjgLh();

        if(fetchOne != null && !StringUtils.isEmpty(fetchOne.getSuperviseObject())) {
            QJgLhjgJcdxEntity jcdx = QJgLhjgJcdxEntity.jgLhjgJcdxEntity;
            JgLhjgJcdxEntity dx = jpaQueryFactory
                    .selectFrom(jcdx)
                    .where(jcdx.enterprisecreditcode.eq(fetchOne.getSuperviseObject())
                    )
                    .fetchFirst();
            vo.setJgLhjgJcdxEntity(dx);
        }

        vo.setJgJgxwCheckActionEntity(fetchOne);

        return vo;
    }

    /**
     * 物理删除
     * @param checkActionEntity
     */
    @Override
    public void physicalDelete(JgJgxwCheckActionEntity checkActionEntity) {
        try {
            kMapper.delete(checkActionEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
